﻿/***********************************************************************
 * <copyright file="UserRepository.cs" company="B/S JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   Hoang Bich Son 
 * Email:    sonhb@newman.vn
 * Website:
 * Create Date: Saturday, March 29, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using DxMvcApplication.Repositories.Core;
using DxMvcApplication.Repositories.Core.Security;
using DxMvcApplication.SecurityServiceReference;
using NmResources;

namespace DxMvcApplication.Repositories.Implementations.Security
{
    /// <summary>
    /// UserRepository class
    /// </summary>
    public class UserRepository : SecurityRepositoryBase, IUserRepository
    {
        public int GetPageSize()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets the list.
        /// </summary>
        /// <param name="criterion">The criterion.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public List<UserDto> GetList(Criterion criterion = null)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets the paged.
        /// </summary>
        /// <param name="totalPage">The total page.</param>
        /// <param name="currentPage">The current page.</param>
        /// <returns></returns>
        public List<UserDto> GetPaged(ref int totalPage, int currentPage = 1)
        {
            var request = new UserRequest().Prepare();
            request.LoadOptions = new[] { "Users" };
            request.OptionTypes = OptionType.IsPaging;
            request.CurrentPage = currentPage;
            request.TotalPage = totalPage;
            var response = SecurityServiceClient.GetUsers(request);
            Correlate(request, response);
            totalPage = response.TotalPage;
            return response.Users == null ? null : response.Users.ToList();
        }

        /// <summary>
        /// Gets the specified identifier.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        public UserDto Get(int id)
        {
            var request = new UserRequest().Prepare();
            request.LoadOptions = new[] { "User", "IncludeRoles" };
            request.Criteria = new UserCriteria { Id = id };
            var response = SecurityServiceClient.GetUsers(request);
            Correlate(request, response);
            return response.User;
        }

        /// <summary>
        /// Inserts the specified t.
        /// </summary>
        /// <param name="t">The t.</param>
        /// <returns></returns>
        /// <exception cref="System.ApplicationException"></exception>
        public string Insert(UserDto t)
        {
            var request = new UserRequest().Prepare();
            request.Action = "Insert";
            request.User = t;
            var response = SecurityServiceClient.SetUsers(request);
            Correlate(request, response);
            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
            var errorMessage = response.MessageCode.MapperResource();
            return errorMessage;
        }

        /// <summary>
        /// Updates the specified t.
        /// </summary>
        /// <param name="t">The t.</param>
        /// <returns></returns>
        /// <exception cref="System.ApplicationException"></exception>
        public string Update(UserDto t)
        {
            var request = new UserRequest().Prepare();
            request.Action = "Update";
            request.User = t;
            var response = SecurityServiceClient.SetUsers(request);
            Correlate(request, response);
            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
            return response.MessageCode.MapperResource();
        }

        /// <summary>
        /// Deletes the specified identifier list.
        /// </summary>
        /// <param name="idList">The identifier list.</param>
        /// <returns></returns>
        /// <exception cref="System.ApplicationException"></exception>
        public string Delete(IList<int> idList)
        {
            var request = new UserRequest().Prepare();
            request.Action = "Delete";
            request.Criteria = new UserCriteria { IdList = string.Join(",", idList) };
            var response = SecurityServiceClient.SetUsers(request);
            Correlate(request, response);
            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
            return response.MessageCode.MapperResource();
        }
    }
}